package system;
import java.sql.*;
public class listening{
	String time;
	int count;
	
	public void setTime(String t){
		time=t;
	}
	public String getTime(){
		return time;
	}
	
	public boolean getInsert(){
		boolean  insert=false;
		String insertCondition="insert into listening values('"+time+"')";
		Connection con;
	    Statement sql;
	    try{
	    	Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
	    	}
	    catch(Exception e){}
   
    try{
		String uri = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test_system";
		String user = "sa";
		String password = "sa";
		con=DriverManager.getConnection(uri,user,password);
		sql=con.createStatement();
		int m=sql.executeUpdate(insertCondition);
		if(m!=0){
			insert=true;			
		}
		con.close();
	}
	catch(SQLException e1){insert=false;}
	return insert;
	}
	
	public int getCount(){
		Connection con;
	    Statement sql;
	    ResultSet rs;
	    try{
	    	Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
	    	}
	    catch(Exception e){}
   
        try{
		String uri = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test_system";
		String user = "sa";
		String password = "sa";
		con=DriverManager.getConnection(uri,user,password);
		sql=con.createStatement();
		rs=sql.executeQuery("select COUNT(*) from listening");
		if(rs.next()){
			count=rs.getInt(1);			
		}
		con.close();
	    } catch(SQLException e1){}
	    return count;
	}
	
	public String[] getTimeList(){
		String[] list=new String[getCount()];
		int index=0;
		
		Connection con;
	    Statement sql;
	    ResultSet rs;
	    try{
	    	Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
	    	}
	    catch(Exception e){}
   
        try{
		String uri = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test_system";
		String user = "sa";
		String password = "sa";
		con=DriverManager.getConnection(uri,user,password);
		sql=con.createStatement();
		rs=sql.executeQuery("select * from listening");
		while(rs.next()){
			list[index]=rs.getString(1);
			index+=1;			
		}
		con.close();
	    } catch(SQLException e1){}
	    return list;
	}
	
}